package com.transfer;

import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingWorker;

import com.transfer.kernel.TextTransfer;

public class TransApplication {

	private JFrame frame;
	private JTextField txtSrc;
	private JTextField txtTarget;
	private final Action action = new SwingAction();
	DefaultListModel<String> listModel = new DefaultListModel<String>();
	private JList<String> list;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TransApplication window = new TransApplication();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public TransApplication() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 696, 435);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		txtSrc = new JTextField();
		txtSrc.setText("D:\\Workspaces\\MyEclipse Blue\\GwSdk");
		txtSrc.setBounds(74, 10, 442, 21);
		frame.getContentPane().add(txtSrc);
		txtSrc.setColumns(10);
		
		JLabel lblSrc = new JLabel("原工程:");
		lblSrc.setBounds(10, 13, 54, 15);
		frame.getContentPane().add(lblSrc);
		
		txtTarget = new JTextField();
		txtTarget.setText("C:\\Users\\Administrator\\Desktop\\GwSdk");
		txtTarget.setBounds(74, 41, 442, 21);
		frame.getContentPane().add(txtTarget);
		txtTarget.setColumns(10);
		
		JLabel lblTaeget = new JLabel("目标工程");
		lblTaeget.setBounds(10, 44, 54, 15);
		frame.getContentPane().add(lblTaeget);
		
		JButton btnSrc = new JButton("...");
		btnSrc.setAction(action);
		btnSrc.setBounds(526, 9, 33, 23);
		btnSrc.setName("src");
		frame.getContentPane().add(btnSrc);
		
		JButton btnGarget = new JButton("...");
		btnGarget.setAction(action);
		btnGarget.setBounds(526, 40, 33, 23);
		btnGarget.setName("target");
		frame.getContentPane().add(btnGarget);
		
		JButton btnStart = new JButton("开始转换");
		btnStart.setAction(action);
		btnStart.setName("start");
		btnStart.setBounds(577, 40, 93, 23);
		frame.getContentPane().add(btnStart);
		
		list = new JList<String>(listModel);
		list.setBounds(10, 72, 660, 315);
		frame.getContentPane().add(list);
	}
	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "SwingAction");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			Object source = e.getSource();
			if (source instanceof JButton) {
				JButton button = JButton.class.cast(source);
				if (button.getName() == "start") {
					// 执行刷新
					frame.setCursor(new Cursor(Cursor.WAIT_CURSOR));
					String src = txtSrc.getText();
					String target = txtTarget.getText();
					if (src.isEmpty() || target.isEmpty()) {
						JOptionPane.showMessageDialog(null, "请先设置路劲", "提示", JOptionPane.INFORMATION_MESSAGE);
						return;
					}
					listModel.clear();
					Worker worker = new Worker(src, target);
					worker.execute();
//					Runnable dealRun = new Runnable(src,tar) {
//						public void run() {
//							try {
//								dealLocalRefresh();
//							} catch (Exception e) {
//								// TODO Auto-generated catch block
//								e.printStackTrace();
//							} finally {
//								frame.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
//							}
//						}
//					};
//					new Thread(dealRun).start();
					return;
				}
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				fileChooser.setDialogTitle("打开文件夹");
				int ret = fileChooser.showOpenDialog(null);
				if (ret == JFileChooser.APPROVE_OPTION) {
					if (button.getName() == "src") {
						txtSrc.setText(fileChooser.getSelectedFile().getAbsolutePath());
					}else if(button.getName() == "target") {
						txtSrc.setText(fileChooser.getSelectedFile().getAbsolutePath());
					}else {
						JOptionPane.showMessageDialog(null, "事件匹配失败"+button.getName(), "警告", JOptionPane.ERROR_MESSAGE); 
					}
				}
			}
		}
		protected void dealLocalRefresh() throws InterruptedException {
			// TODO Auto-generated method stub
			Thread.sleep(5000);
		}
	}
	
	private class Worker extends SwingWorker<Void, String> {

		private String src;
		private String target;
		
		public Worker(String src,String target) {
			// TODO Auto-generated constructor stub
			this.src = src;
			this.target = target;
		}
		
		@Override
		protected Void doInBackground() throws Exception {
			// TODO Auto-generated method stub
			File fsrc = new File(src);
			File ftarget = new File(target);
			if (!fsrc.exists()) {
				publish("源项目不存在！");
				return null;
			}
			if (!ftarget.exists() && !ftarget.mkdirs()) {
				publish("创建目标项目失败！");
				return null;
			}
			try {
				TextTransfer transfer = new TextTransfer(fsrc,ftarget);
				while (transfer.hasTask()) {
					publish(transfer.doTask());
				}
			} catch (Exception e) {
				// TODO: handle exception
				publish(e.getClass()+"-"+e.getMessage());
			}
			return null;
		}

		@Override
		protected void process(List<String> chunks) {
			// TODO Auto-generated method stub
			for (String string : chunks) {
				listModel.add(0,string);
			}
		};

		@Override
		protected void done() {
			// TODO Auto-generated method stub
			frame.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
			super.done();
		}
		
		
	};
}
